﻿using System;
using System.IO;
using System.Windows.Forms;
using Ini;
using WindowsHelper;
using Microsoft.Win32;
using TaskScheduler;
using System.Diagnostics;

namespace GlobalHooksTest
{
    public partial class Options : Form
    {
        public Options()
        {
            InitializeComponent();
        }

        public static IniFile SettingsIni = new IniFile(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Sbar.ini");
        public static RegistryKey rkCurrentUser = Registry.CurrentUser; 
        private int sbXPosI = Convert.ToInt32(SettingsIni.IniReadValue("Main", "XPos"));//Properties.Settings.Default.XPos;
        private int sbYPosI = Convert.ToInt32(SettingsIni.IniReadValue("Main", "YPos"));
        private void TrackBarThu_ValueChanged(object sender, EventArgs e)
        {

            ThuSize.Text = TrackBarThu.Value + " px  x  " + TrackBarThu.Value + " px";
        }

        private void Options_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((sender as Form).DialogResult == System.Windows.Forms.DialogResult.OK)
            {

                SettingsIni.IniWriteValue("Main", "TrackBarThu", TrackBarThu.Value.ToString());
                SettingsIni.IniWriteValue("Main", "ThuSize", TrackBarThu.Value.ToString());
                SettingsIni.IniWriteValue("Main", "Skin", SkinCombo.Text);
                SettingsIni.IniWriteValue("Main", "WorkAreaRes", Convert.ToInt32(chkWorkAreaRes.Checked).ToString());
                SettingsIni.IniWriteValue("Main", "InTaskbar", Convert.ToInt32(chkInTaskbar.Checked).ToString());
                SettingsIni.IniWriteValue("Main", "Count", Convert.ToInt32(!chkBadgeCount.Checked).ToString());
                SettingsIni.IniWriteValue("Main", "TaskbarHide", Convert.ToInt32(chkTaskbarHide.Checked).ToString());
                SettingsIni.IniWriteValue("Main", "XPos", sbXPos.Value.ToString());
                SettingsIni.IniWriteValue("Main", "YPos", sbYPos.Value.ToString());
                SettingsIni.IniWriteValue("Main", "DisableThu", Convert.ToInt32(chkThumbnailDisable.Checked).ToString());
                SettingsIni.IniWriteValue("Main","ThuSpeed",edtThuSpeed.Text);
                if (chkThumbnailDisable.Checked)
                {
                    frmSbarDevelopper.ThuDisable = true;
                }
                else
                {
                    frmSbarDevelopper.ThuDisable = false;
                }

                if (frmSbarDevelopper.sv.OSMajorVersion != 6)
                {
                    if (chkAutoStart.Checked)
                    {
                        if (!File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk")))
                        {
                            File.Copy(Path.GetDirectoryName(Application.ExecutablePath) + @"\SBar.lnk", Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk"));
                        }
                    }
                    else
                    {
                        if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk")))
                        {
                            File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk"));
                        }
                    } 
                }
                else
                {
//                    if (chkAutoStart.Checked)
//                    {
//                        if (!File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk")))
//                        {
//                            File.Copy(Path.GetDirectoryName(Application.ExecutablePath) + @"\SBar.lnk", Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk"));
//                        }
//                    }
//                    else
//                    {
//                        if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk")))
//                        {
//                            File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk"));
//                        }
//                    } 
                    var scheduler = new TaskScheduler.TaskSchedulerClass();
                    scheduler.Connect(null, null, null, null);
                    if ((chkAutoStart.Checked))
                    {     
                        ITaskDefinition task = scheduler.NewTask(0);
                        task.RegistrationInfo.Author = "DimitarCC";
                        task.RegistrationInfo.Description = "Start SBar on startup";
                        task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
                        task.Settings.StopIfGoingOnBatteries = false;
                        task.Settings.DisallowStartIfOnBatteries = false;
                        task.Settings.ExecutionTimeLimit = "";
                        ILogonTrigger trigger = (ILogonTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
                        trigger.UserId = Environment.UserName;
                        trigger.Id = "SBar start trigger";
                        
                        IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
                        action.Id = "SBar start action";
                        action.Path = Application.ExecutablePath;
                        action.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
                        ITaskFolder rootf = scheduler.GetFolder("\\");
                        IRegisteredTask regTask = rootf.RegisterTaskDefinition("SBar", task,
                                                                               (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
                                                                               null, null,
                                                                               _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN,
                                                                               ""); 
                        
                        
                    }
                    else
                    {

                        ITaskFolder rootf = scheduler.GetFolder("\\");
                        rootf.DeleteTask("SBar",0);
//                        rootf.DeleteFolder("SBar",0);
                    }
                    
                }

                if (sbXPosI != sbXPos.Value | sbYPosI != sbYPos.Value)
                {
                    frmSbarDevelopper.RepositionSbar(sbXPosI - (int)sbXPos.Value, sbYPosI - (int)sbYPos.Value);
                    sbXPosI = (int)sbXPos.Value;
                    sbYPosI = (int)sbYPos.Value;
                }
                
//                Properties.Settings.Default.TaskbarHide = chkTaskbarHide.Checked;
//                Properties.Settings.Default.WorkAreaReserve = chkWorkAreaRes.Checked;
//                Properties.Settings.Default.Skin = SkinCombo.Text;
//                Properties.Settings.Default.ThumbnailSize = TrackBarThu.Value;
//                Properties.Settings.Default.InTaskbar = chkInTaskbar.Checked;
//                Properties.Settings.Default.XPos = (int) sbXPos.Value;
//                Properties.Settings.Default.YPos = (int)sbYPos.Value;
//                Properties.Settings.Default.Save(); 
            }
//            Form1.EnableTimer();
        }

        private void Options_Load(object sender, EventArgs e)
        {
            SkinCombo.Items.Clear();
            if (SettingsIni.IniReadValue("Main", "TrackBarThu")== "" | SettingsIni.IniReadValue("Main", "TrackBarThu") == null)
            {
                TrackBarThu.Value = 150;
                ThuSize.Text = "150" + " px  x  " + "150" + " px";
            }
            else
            {
                TrackBarThu.Value = Convert.ToInt32(SettingsIni.IniReadValue("Main", "TrackBarThu"));//Properties.Settings.Default.ThumbnailSize; 
//                ThuSize.Text = TrackBarThu.Value + " px  x  " + TrackBarThu.Value + " px";
            }
            if (SettingsIni.IniReadValue("Main", "ThuSpeed") != null & SettingsIni.IniReadValue("Main", "ThuSpeed") != "")
            {
                edtThuSpeed.Value = Convert.ToInt32(SettingsIni.IniReadValue("Main", "ThuSpeed"));
            }
            else
            {
                SettingsIni.IniWriteValue("Main", "ThuSpeed","1000");
                edtThuSpeed.Value = 1000; 
            }
            
//            ThuSize.Text = SettingsIni.IniReadValue("Main", "ThuSize");//Properties.Settings.Default.ThumbnailSize.ToString();
            string PathExe = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            var di = new DirectoryInfo(PathExe + "\\Skins");
            System.IO.DirectoryInfo[] fi = di.GetDirectories();
            foreach (DirectoryInfo info in fi)
            {
                SkinCombo.Items.Add(info.Name);
            }
            if (SettingsIni.IniReadValue("Main", "Skin") == "" | SettingsIni.IniReadValue("Main", "Skin") == null)
            {
                SkinCombo.Text = "Aero";
            }
            else
            {
                SkinCombo.Text = SettingsIni.IniReadValue("Main", "Skin"); //Properties.Settings.Default.Skin;
            }
            if (SettingsIni.IniReadValue("Main", "WorkAreaRes") == "" | SettingsIni.IniReadValue("Main", "WorkAreaRes") == null)
            {
                chkWorkAreaRes.Checked = false;
            }
            else
            {
                chkWorkAreaRes.Checked =
                    Convert.ToBoolean(Convert.ToInt32(SettingsIni.IniReadValue("Main", "WorkAreaRes")));
                    //Properties.Settings.Default.WorkAreaReserve;
            }
            if (SettingsIni.IniReadValue("Main", "DisableThu") == "" | SettingsIni.IniReadValue("Main", "DisableThu") == null)
            {
                chkThumbnailDisable.Checked = false;
            }
            else
            {
                chkThumbnailDisable.Checked =
                    Convert.ToBoolean(Convert.ToInt32(SettingsIni.IniReadValue("Main", "DisableThu")));
                //Properties.Settings.Default.WorkAreaReserve;
            }
            if (SettingsIni.IniReadValue("Main", "Count") == "" | SettingsIni.IniReadValue("Main", "Count") == null)
            {
                chkBadgeCount.Checked = false;
            }
            else
            {
                chkBadgeCount.Checked =
                    !Convert.ToBoolean(Convert.ToInt32(SettingsIni.IniReadValue("Main", "Count")));
                //Properties.Settings.Default.WorkAreaReserve;
            }
            if (SettingsIni.IniReadValue("Main", "InTaskbar") == "" | SettingsIni.IniReadValue("Main", "InTaskbar") == null)
            {
                chkInTaskbar.Checked = true;
            }
            else
            {
                chkInTaskbar.Checked = Convert.ToBoolean(Convert.ToInt32(SettingsIni.IniReadValue("Main", "InTaskbar")));
                    //Properties.Settings.Default.InTaskbar;
            }
            chkWorkAreaRes.Enabled = !chkInTaskbar.Enabled;
            if (SettingsIni.IniReadValue("Main", "TaskbarHide") == "" | SettingsIni.IniReadValue("Main", "TaskbarHide") == null)
            {
                chkTaskbarHide.Checked = true;
            }
            else
            {
                chkTaskbarHide.Checked =
                    Convert.ToBoolean(Convert.ToInt32(SettingsIni.IniReadValue("Main", "TaskbarHide")));
                    //Properties.Settings.Default.TaskbarHide;
            }
            if (SettingsIni.IniReadValue("Main", "XPos") == "" | SettingsIni.IniReadValue("Main", "XPos") == null)
            {
                WindowsAPI.RECTW TaskPos = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(frmSbarDevelopper.hReBar, ref TaskPos);
                WindowsAPI.RECTW TaskHPos = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(frmSbarDevelopper.hTask, ref TaskHPos);
                WindowsAPI.RECTW QLPos = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(frmSbarDevelopper.hQuickLaunch, ref QLPos);
                //            MessageBox.Show("QLLeft= " + QLPos.Left + " - " + "AppL = " + TaskHPos.Left + "Left = " + TaskPos.Left);
                if (frmSbarDevelopper.hQuickLaunch != IntPtr.Zero)
                {
                    if (TaskPos.Height != TaskHPos.Height)
                    {
                        sbYPos.Value = QLPos.Bottom + 2;
                        sbXPos.Value = TaskPos.Left;
                    }
                    else
                    {
                        sbYPos.Value = TaskPos.Top + 2;
                        if (QLPos.Left < TaskHPos.Left)
                        {
                            sbXPos.Value = TaskHPos.Left;
                        }
                        else
                        {
                            sbXPos.Value = TaskPos.Left;
                        }

                    }
                }
                else
                {
                    sbXPos.Value = TaskPos.Left;
                    sbYPos.Value = TaskPos.Top + 2;
                }
            }
            else
            {
                sbXPos.Value = Convert.ToInt32(SettingsIni.IniReadValue("Main", "XPos"));//Properties.Settings.Default.XPos;
                sbYPos.Value = Convert.ToInt32(SettingsIni.IniReadValue("Main", "YPos"));//Properties.Settings.Default.YPos;  
            }
//            sbXPos.Value = Convert.ToInt32(SettingsIni.IniReadValue("Main", "XPos"));//Properties.Settings.Default.XPos;
//            sbYPos.Value = Convert.ToInt32(SettingsIni.IniReadValue("Main", "YPos"));//Properties.Settings.Default.YPos;
            if (frmSbarDevelopper.sv.OSMajorVersion != 6)
            {
                if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "Sbar.lnk")))
                {
                    chkAutoStart.Checked = true;
                }
                else
                {
                    chkAutoStart.Checked = false;
                }
            }
            else
            {
                var scheduler = new TaskScheduler.TaskSchedulerClass();
                scheduler.Connect(null, null, null, null);
                ITaskFolder SbarFolder = scheduler.GetFolder("\\");
                IRegisteredTask SBarTask = SbarFolder.GetTask("SBar");
                bool IsAvailable = false;
                if (SBarTask != null)
                {
                    ILogonTrigger tr = (ILogonTrigger) SBarTask.Definition.Triggers[1];
                    if (tr.UserId == Environment.UserName)
                    {
                        IsAvailable = true;
                    }
                    else
                    {
                        IsAvailable = false;
                    }
                }
                if (IsAvailable)
                {
                    chkAutoStart.Checked = true;
                }
                else
                {
                    chkAutoStart.Checked = false;
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            WindowsAPI.RECTW TaskPos = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(frmSbarDevelopper.hReBar, ref TaskPos);
            WindowsAPI.RECTW TaskHPos = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(frmSbarDevelopper.hTask, ref TaskHPos);
            WindowsAPI.RECTW QLPos = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(frmSbarDevelopper.hQuickLaunch, ref QLPos);
//            MessageBox.Show("QLLeft= " + QLPos.Left + " - " + "AppL = " + TaskHPos.Left + "Left = " + TaskPos.Left);
            if (frmSbarDevelopper.hQuickLaunch != IntPtr.Zero)
            {
                if (TaskPos.Height != TaskHPos.Height )
                {
                    sbYPos.Value = QLPos.Bottom + 2;
                    sbXPos.Value = TaskPos.Left;
                }
                else
                {
                    sbYPos.Value = TaskPos.Top + 2;
                    if (QLPos.Left < TaskHPos.Left)
                    {
                        sbXPos.Value = TaskHPos.Left;
                    }
                    else
                    {
                        sbXPos.Value = TaskPos.Left;
                    }
                    
                }
            }
            else
            {
              sbXPos.Value = TaskPos.Left;
              sbYPos.Value = TaskPos.Top + 2;  
            }
            
            
            TrackBarThu.Value = 150;
            SkinCombo.Text = "Aero";
//            chkWorkAreaRes.Checked = true;
            chkInTaskbar.Checked = true;
            chkTaskbarHide.Checked = true;
            chkBadgeCount.Checked = false;
            chkThumbnailDisable.Checked = false;
            edtThuSpeed.Value = 1000;
            
        }

        private void Options_Activated(object sender, EventArgs e)
        {
//            Form1.DiableTimer();
        }

        private void chkTaskbarHide_CheckedChanged(object sender, EventArgs e)
        {
            frmSbarDevelopper.ShowHideTaskbar(!(sender as CheckBox).Checked);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if ((sender as CheckBox).Checked)
            {
                chkWorkAreaRes.Enabled = false;  
            }
            else
            {
                chkWorkAreaRes.Enabled = true;
                
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            SettingsIni.IniWriteValue("Main", "TrackBarThu", TrackBarThu.Value.ToString());
            SettingsIni.IniWriteValue("Main", "ThuSize", TrackBarThu.Value.ToString());
            SettingsIni.IniWriteValue("Main", "Skin", SkinCombo.Text);
            SettingsIni.IniWriteValue("Main", "WorkAreaRes", Convert.ToInt32(chkWorkAreaRes.Checked).ToString());
            SettingsIni.IniWriteValue("Main", "InTaskbar", Convert.ToInt32(chkInTaskbar.Checked).ToString());
            SettingsIni.IniWriteValue("Main", "TaskbarHide", Convert.ToInt32(chkTaskbarHide.Checked).ToString());
            SettingsIni.IniWriteValue("Main", "Count", Convert.ToInt32(chkBadgeCount.Checked).ToString());
            SettingsIni.IniWriteValue("Main", "XPos", sbXPos.Value.ToString());
            SettingsIni.IniWriteValue("Main", "YPos", sbYPos.Value.ToString());
            SettingsIni.IniWriteValue("Main", "DisableThu", Convert.ToInt32(chkThumbnailDisable.Checked).ToString());
            if (sbXPosI != sbXPos.Value | sbYPosI != sbYPos.Value)
            {
                frmSbarDevelopper.RepositionSbar(sbXPosI -(int)sbXPos.Value , sbYPosI -(int)sbYPos.Value);
                sbXPosI = (int)sbXPos.Value;
                sbYPosI = (int)sbYPos.Value;
            }

        }

        private void chkThumbnailDisable_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void btnReorder_Click(object sender, EventArgs e)
        {
            ReorderPinnedItems RPI = new ReorderPinnedItems();
            RPI.ShowDialog();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

        }

        private void btnExclusion_Click(object sender, EventArgs e)
        {
            ExclusionList ExclusionListForm = new ExclusionList();
            ExclusionListForm.ShowDialog();
        }

        private void btnSkinEdit_Click(object sender, EventArgs e)
        {
            Process.Start(frmSbarDevelopper.ExePath + "\\Skins\\" + frmSbarDevelopper.SkinDir + "\\Skin.ini");
        }
    }
}
